//
//  StatusListModel.swift
//  sinaOnceAgain
//
//  Created by 王相超 on 16/2/24.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking

class StatusListModel: NSObject {
    
    
    //模型数组
    lazy var statuses = [Status]()
    
    
    func loadHomeData(isPullUp: Bool, finished: (isSuccess: Bool) -> ()) {
            
            let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
            
            guard let token = UserAccountViewModel.sharedAccountViewModel.access_token else {
                SVProgressHUD.showErrorWithStatus("请登录")
                
                //执行失败的回调
                finished(isSuccess: false)

                return
            }
            
            var parameters = ["access_token": token]
            var since_id: Int64 = 0
            var max_id: Int64 = 0
            
            if isPullUp {
                
                max_id = self.statuses.last?.id ?? 0
            }else {
                
                since_id = self.statuses.first?.id ?? 0
            }
            
        
            
            if since_id > 0{
                
                parameters["since_id"] = "\(since_id)"
            }
            if max_id > 0 {
                parameters["max_id"] = "\(max_id-1)"
            }
        
        
        HTTPClient.sharedClient.requestJSONDict(.GET, urlString: urlString, parameters: parameters) { (dict, error) -> () in
            
            if error != nil {
            
            finished(isSuccess: false)
            return
            }
            
            //在字典中 通过 "statuses"这个key 来获取微博数组数据
            guard let array = dict!["statuses"] as? [[String: AnyObject]] else {
                //执行失败的回调
                finished(isSuccess: false)
                
                return
            }
            
            //实例化模型数组
            var temArr = [Status]()
            
            //遍历数组  获取单个的字典对象
            for item in array {
                
                //字典转模型
                let s = Status(dict: item)
                
                //将模型添加到数组中
                temArr.append(s)
            }
            //记录临时数组
            
            
            if since_id > 0{
                
                self.statuses = temArr + self.statuses
            }else if max_id > 0 {
                //上拉加载更多数据
                self.statuses += temArr
                
            }else{
                
                self.statuses = temArr
            }
            
            //执行成功的回调
            finished(isSuccess: true)
    
            //发起get请求
    
//             let AFN = AFHTTPSessionManager()
//            AFN.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
//
//
//                //将result 转换成 字典类型
//                guard let dict = result as? [String: AnyObject] else {
//                    //执行失败的回调
//                    finished(isSuccess: false)
//
//                    return
//                }
//
//                //在字典中 通过 "statuses"这个key 来获取微博数组数据
//                guard let array = dict["statuses"] as? [[String: AnyObject]] else {
//                    //执行失败的回调
//                    finished(isSuccess: false)
//
//                    return
//                }
//                
//                //实例化模型数组
//                var temArr = [Status]()
//                
//                //遍历数组  获取单个的字典对象
//                for item in array {
//                    
//                    //字典转模型
//                    let s = Status(dict: item)
//                    
//                    //将模型添加到数组中
//                    temArr.append(s)
//                }
//                //记录临时数组
//                
//                
//                if since_id > 0{
//                    
//                    self.statuses = temArr + self.statuses
//                }else if max_id > 0 {
//                    //上拉加载更多数据
//                    self.statuses += temArr
//                    
//                }else{
//                    
//                    self.statuses = temArr
//                }
//                
//                //执行成功的回调
//                finished(isSuccess: true)
//                }) { (_, error) -> Void in
//                    
//                    //执行失败的回调
//                    finished(isSuccess: false)
//                    SVProgressHUD.showErrorWithStatus("网络错误,请稍候再试")
//                    print(error)
//            }
    

    }
    }
}
